
<!--  Sales Voucher -->

<%@page import="java.io.PrintWriter"%>
<%@page import="java.sql.ResultSet" import="java.sql.Statement" import="java.sql.Connection" import="connection.DBConnection"%>
<%@page import="java.util.*" %>
<%@page import="java.text.SimpleDateFormat" %> 
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Sales Voucher</title>

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>
$(function() 
{
	var dialog, form,
			
	c1 = $( "#customername" ),	c2 = $( "#customercode" ),	allFields = $( [] ).add( c1 ).add( c2 ), tips = $( ".validateTips" );
	
	function updateTips( t ) 
	{
		tips.text( t ).addClass( "ui-state-highlight" );
		
		setTimeout(function() 
		{
			tips.removeClass( "ui-state-highlight", 1500 );
		}, 500 );
	}
	
	function checkLength( o, n, min, max ) 
	{
		if ( o.val().length > max || o.val().length < min ) 
		{
			o.addClass( "ui-state-error" );
			updateTips( "Length of " + n + " must be between " +
			min + " and " + max + "." );
	
			return false;
		} 
		else {
			return true;
			}
	}
	
	function checkRegexp( o, regexp, n ) 
	{
		if ( !( regexp.test( o.val() ) ) ) 
		{
			o.addClass( "ui-state-error" );
			updateTips( n );
			
			return false;
		} 
		else {	
			return true;
			}
	}

	function addUser() 
	{
		var valid = true;
	
		allFields.removeClass( "ui-state-error" );
		valid = valid && checkLength( c1, "customername", 3, 20 );
		valid = valid && checkLength( c2, "customercode", 1, 5 );


		if ( valid ) 
		{
			$( "#custname" ).append( "<select>" +	"<option>" + c1.val() + "</option>" +	"</select>" );
			dialog.dialog( "close" );
		}

		return valid;
	}
	
	dialog = $( "#dialog-form" ).dialog({
		autoOpen: false,
		height: 300,
		width: 350,
		modal: true,
	
		buttons: 
		{
			"Create an account": addUser,
			Cancel: function() 
			{
				dialog.dialog( "close" );	
			}
		},
	
		close: function() 
		{
			form[ 0 ].reset();
			allFields.removeClass( "ui-state-error" );
		}
	});
	
	form = dialog.find( "form" ).on( "submit", function( event ) 
	{
		event.preventDefault();
		addUser();
	});
	
	$( "#create-user" ).button().on( "click", function() 
	{
		dialog.dialog( "open" );
	});
	
});
	
</script>


<script language="javascript" type="text/javascript"> 

function checkDate()
{
	var c1;
	
  	var datevalid=/^(\d{4})(\/|-)(\d{1,2})(\/|-)(\d{1,2})$/;
  		
        c1= document.f14.rdate.value;

        if(c1.length==0)
        {
        	alert("Enter Date");
            return false;
      	 }
        if(!c1.match(datevalid))
        {
            alert("Please Enter Date in YYYY/MM/DD Format");
            document.f14.rdate.value="";
            document.f14.rdate.focus();
            
	         return false;
    	 }
        else
        {
        	dateval=c1.match(datevalid);
        }
        
        var dtYear = dateval[1];        
        dtMonth = dateval[3];
        dtDay=  dateval[5];

     if (dtMonth < 1 || dtMonth > 12) 
        return false;
     else if (dtDay < 1 || dtDay> 31) 
       return false;
     else if ((dtMonth==4 || dtMonth==6 || dtMonth==9 || dtMonth==11) && dtDay ==31) 
       return false;
     else if (dtMonth == 2){ 
       var isleap = (dtYear % 4 == 0 && (dtYear % 100 != 0 || dtYear % 400 == 0));
       if (dtDay> 29 || (dtDay ==29 && !isleap)) 
      	 
      	 alert("Date format is not correct");
       document.f14.rdate.value="";
       document.f14.rdate.focus();
              return false;
        
        return true;
}

</script>

<script language="javascript" type="text/javascript"> 

function populateCustomerId(){
    var custname = document.getElementById('custname');

    /* selected value of dropdown */
    var selectedCustomerId = custname.options[custname.selectedIndex].value;

    /* selected value set to input field */
    document.getElementById('custcode').value = selectedCustomerId; 
}
     
</script>


<script language="javascript" type="text/javascript">

function NetAmount()
{
    var p1 = document.getElementById("prodquantity").value;
    var p2 = document.getElementById("prodrate").value;
   
    var p3=parseInt(p1) * parseFloat(p2);
 
    document.getElementById("netamount").value = p3; 
}
    
</script>   


<script language="javascript" type="text/javascript">

function TaxAmount(opt)
{
	var tax1 = document.getElementById("netamount").value;
	var tax2= opt.text;
	
	var nettax= (tax1 * tax2) /100;
	document.getElementById("taxamount").value = nettax;	
}
</script>

<script language="javascript" type="text/javascript">

function TotalAmount()
{
	var t1 = document.getElementById("netamount").value;
	var t2 = document.getElementById("taxamount").value;
	var t3 = document.getElementById("transport").value;
	
	var total=parseFloat(t1)+parseFloat(t2)+parseFloat(t3);
	
	document.getElementById("totalamount").value = total;

}
	
</script>

</head>
<body>


<div id="dialog-form" title="Create new user">
<p class="validateTips"></p>
<form  name="f1" action="CustomerMasterServlet" >
<fieldset>

<table >
	
<tr>
	<th>	</th>
	<th>	</th>
</tr>

<tr>
	<td> Customer Name </td>
	<td> <input type="text" name="customername" class="text ui-widget-content ui-corner-all"> </td>
</tr>

<tr>
	<td> Customer Code </td>
	
				<%	DBConnection dbc=new DBConnection();	
				 	Connection con=dbc.getNewConnection();
				 	String code2="C1";
			
				 Statement st = null;
				 ResultSet rs = null;
			try
			{
			   st=con.createStatement() ;
       		   rs=st.executeQuery("select ccode from CustomerMaster"); %>
       		   
       		 <%  while(rs.next()) 
       		 	{ 
       				String code1=rs.getString(1);
       				
       			   String[] part = code1.split("(?<=\\D)(?=\\d)");
	   		       
	   		       String p0=part[0];		       
	   		       int p1=Integer.parseInt(part[1]);
	   		      
	   		       p1++;
	   		       
	   		       code2=p0+""+p1;

            	} %>
  
	 		<% }
		   catch(Exception e){} %>
		
	<td> <input type="text" name="customercode" value="<%=code2 %>" class="text ui-widget-content ui-corner-all"> </td>
</tr>

<tr>
	<td> Address</td>
	<td> <input type="text" name="customeraddress"> </td>
</tr>

<tr>
	<td> City </td>
	<td> <input type="text" name="customercity"> </td>
</tr>

<tr>
	<td> State </td>
	<td> <input type="text" name="customerstate"> </td>
</tr>

<tr>
	<td> TIN Number </td>
	<td> <input type="text" name="customertin"> </td>
</tr>

<tr>
	<td> PAN Number</td>
	<td> <input type="text" name="customerpan"> </td>
</tr>

<tr>
	<td> Mobile Number</td>
	<td> <input type="text" name="customermobile"> </td>
</tr>

<tr>
	<td> Email ID </td>
	<td> <input type="text" name="customeremail"> </td>
</tr>

<tr>
	<td> Contact Person Name </td>
	<td> <input type="text" name="customercontactperson"> </td>
</tr>

<tr>
	<td> Opening Balance </td>
	<td> <input type="text" name="copeningbalance"> </td>
</tr>

<tr>
	
	<td> <input type="submit" name="enter" value="Submit"> </td>
</tr>

</table>
	</fieldset>
</form>
</div>

<div id="users-contain" class="ui-widget">
<h1>Sales Voucher</h1>
<form name="f11"  action=SalesVoucherServlet > 

<table>
<tr>
	<td> Against Sales order number </td>
	<td> <input type="text" name="ordnum"/> </td>
<tr>
	<td>Invoice number</td>
	
			<%	
			 	String code3="SV1";
			try
			{
			   st=con.createStatement() ;
       		   rs=st.executeQuery("select MAX(innum) from SalesVoucher2"); %>
       		   
       		 <%  while(rs.next()) 
       		 	{ 
       				String code1=rs.getString(1);
       				
       			   String[] part = code1.split("(?<=\\D)(?=\\d)");
	   		       
	   		       String p0=part[0];		       
	   		       int p1=Integer.parseInt(part[1]);
	   		      
	   		       p1++;
	   		       
	   		       code2=p0+""+p1;
	   		       
            	}  %>
  
	 		<% }
		   catch(Exception e){} %>
		   
	<td><input type="text" name="Innum" value="<%=code2 %>"/></td>
	
	<td>Date</td>

	<td><input type="text" name="dte"  /></td>
	
</tr>

<tr>
	<td>Name of Customer</td>
	<!--  <td><input type="text" name="custname"/></td> -->
	
			<%	
			try
			{
			   st=con.createStatement() ;
       		   rs=st.executeQuery("select ccode,cname from CustomerMaster"); %>
			
			<td> 
	  			<select name="custname" id="custname" onchange="populateCustomerId();">
	  				<option> </option>
       		 		<%  while(rs.next()){ %>
            			<option value="<%=rs.getString(1)%>" ><%= rs.getString(2)%></option>
      
     			    <% } %>
      			</select>
    		</td>
  
			<% }
			catch(Exception e){} %>
	
	 <td><a href="CustomerMaster.jsp"> Create New Customer </a></td>
	<!-- <td><input type=button onClick=window.open("CustomerMaster.jsp","Ratting","width=550, height=170,left=150,top=200,toolbar=0,status=0,"); value="Open Window"></td> -->
	<td><input type="submit" name="act" value="Refresh account names"/>
</tr>

<tr>
	<td>Customer code</td>
	<td><input type="text" name="custcode" id="custcode" />
	<td>Customer Invoice number</td>
	<td><input type="text" name="custinnum" /> </td>
	</tr>
	
<tr>
	<td>Product Name</td>
	<!-- <td><select name="prodname"></select></td> -->
	
		 <%	 
			try
			{
			   st=con.createStatement() ;
       		   rs=st.executeQuery("select stname from StockMaster"); %>
	<td> 
	  <select name="prodname">
	  	<option> </option>
       		 <%  while(rs.next()){ %>
            		<option><%= rs.getString(1)%></option>
      
          <% } %>
       </select>
     </td>
  
	 <% }
		catch(Exception e){} %>
	
	<td><a href="StockMaster.jsp"> Create New Stock </a></td>
	<td><input type="submit" name="act" value="Refresh Item names"/></td>
	</tr>
	
<tr>
	<td>Product Quantity</td>
	<td><input type="text" name="prodquantity" id="prodquantity"  /></td>
	
	<td>Product Rate</td>
	<td><input type="text" name="prodrate" id="prodrate" /></td>
</tr>

<tr> 
	<td>Net Amount</td>
	<td><input type="text" name="netamount" id="netamount" /></td>
	
	<td><input type="button" value="Calculate Net Amount"  onClick="NetAmount();"/></td>
	
</tr>
<tr>
	<td>Taxation Category</td>
	<td>
	<select name="taxcat" id="taxcat" onchange="TaxAmount((this.options[this.selectedIndex]));" >
	<option> </option>
	<option value="5.0"> 5.0 </option>
	<option value="12.5"> 12.5 </option>
	</select>
	</td>
</tr>
	
<tr>
	<td>Tax Amount</td>
	<td><input type="text" name="taxamount" id="taxamount" /></td>
	<td><input type="button" name="act" value="Calculate Tax Amount" /></td>
</tr>
	
<tr>
	<td>Transport Charges</td>
	<td> <input type="text" name="transport" id="transport" /></td>
</tr>
	
<tr>
	<td>Total Amount</td>
	<td><input type="text" name="totalamount" id="totalamount" /></td>
	<td><input type="button" name="act" value="Calculate Total Amount" onClick="TotalAmount();" /></td>
</tr>
	
<tr>
	<td>Credit limit (No. of Days)</td>
	<td><input type="text" name="credlimit"/></td>
	</tr>
<tr>
	<td><input type="submit" name="enter" value="Enter" /></td>
</tr>
</table>

<input type=button id="create-user" value="Create new Customer">

</form>
</div>
</body>
</html>